package com.e3.template.tgf;

import java.util.List;

import com.e3.template.model.TGFTreeModel;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.client.TreeListener;
import com.e3.web.core.model.SuperModel;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyMenu;
import com.e3.web.widget.client.MyMenuItem;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class MyTGFExUI<D extends SuperModel,M extends TGFTreeModel> extends SuperGFUI<D> implements TreeListener<M> {
	private ContentPanel contentPanel = new ContentPanel(new BorderLayout());
	private TGFTreePanel<M> treePanel;
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		this.setLayout(new FitLayout());
		
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.WEST);
		bld.setSplit(true);
		bld.setMargins(new Margins(0,2,0,0));
		contentPanel.add(getTreePanel(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getRightContentPanel(),bld);
		
		getRightContentPanel().setHeaderVisible(false);
		contentPanel.setHeaderVisible(false);
		
		bld = new BorderLayoutData(LayoutRegion.NORTH);
		bld.setSplit(true);
		bld.setMargins(new Margins(0,2,0,0));
		getRightContentPanel().add(getGridPanel(),bld);
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		getRightContentPanel().add(getFormPanel(),bld);
		getFormPanel().setFormReadonly(true);
		
		initToolBar();
		contentPanel.setTopComponent(getToolbar());
		this.add(contentPanel);
	}
	
	public TGFTreePanel<M> getTreePanel() {
		if(treePanel == null){
			treePanel = createTreePanel();
			treePanel.addTreeListener(this);
			treePanel.setTreeContextMenu(getTreeContextMenu());
			treePanel.setMaskComponent(this);		
		}
		return treePanel;
	}
	
	protected Menu getTreeContextMenu(){
		MyMenu menu = new MyMenu();

		MyMenuItem item = new MyMenuItem(TGFStateMachine.REFRESH_ACTION);
		item.addSelectionListener(getMnuSelectListener());
		menu.add(item);

		return menu;
	}

	protected List<D> getCurrentSelectedModels(){
		return getGridPanel().getSelectedModels();
	}
	
	public void onEditSuccess(D model) {
		getGridPanel().refreshGridModel(model);
	}
	
	protected void onDeleteSuccess(List<D> models){
		getGridPanel().removeValues(models);
	}

	protected D getCurrentModel(BaseAction action){
		return getGridSelectedModel();
	}
	
	public void onTreeDataSelected(M model, List<M> subModels) {
		if(model!=null){
			executeQueryService(model,getQueryAsyncCallback());
		}
	}
	
	protected AsyncCallback<List<D>> getQueryAsyncCallback() {
		return new GwtAsyncCallback<List<D>>(this) {
			@Override
			public void doSuccess(List<D> result) {
				onQuerySuccess(result);
			}
		};
	}

	protected void onQuerySuccess(List<D> models) {
		getGridPanel().setGridValues(models);
	}

	protected abstract TGFTreePanel<M> createTreePanel();
	
	protected abstract void executeQueryService(M model,AsyncCallback<List<D>> callback);
}